package com.cj.controller;

import com.cj.Service.UserService;
import com.cj.annotation.ControllerLog;
import com.cj.pojo.DeptPojo;
import com.cj.pojo.UserPojo;
import com.cj.vo.JsonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.authority.AuthorityUtils;
import org.springframework.security.core.userdetails.User;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
public class UserController {
    @Autowired
    UserService userService;

    @GetMapping("/login")
    public String userLogin(){
        return  "pages/login";
    }

    @RequestMapping("/user/index")
    public String index(){
        return "pages/content/user_list";
    }

    // 查询账号信息
    @RequestMapping("/user/list")
    @ResponseBody
    public JsonResult userList(String key){
//        UserPojo dp = new UserPojo();
        List<UserPojo> userList = userService.queryUserListByKey(key);
        return  new JsonResult(1,"成功" ,userList);
    }

    @RequestMapping(value = "/user/add")
    @ResponseBody
    public JsonResult userAdd(UserPojo userPojo){
//        UserPojo dp = new UserPojo();
        try{
            userService.addUser(userPojo);
        } catch (Exception e) {
            e.printStackTrace();
            return  new JsonResult(0,"失败" ,null);
        }
        return  new JsonResult(1,"成功" ,null);
    }

    @RequestMapping(value = "/user/update")
    @ResponseBody
    public JsonResult userUpdate(UserPojo userPojo){
//        UserPojo dp = new UserPojo();
        try{
            userService.updateUser(userPojo);
        } catch (Exception e) {
            e.printStackTrace();
            return  new JsonResult(0,"失败" ,null);
        }
        return  new JsonResult(1,"成功" ,null);
    }

}
